define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'check/cleaner/index',
                    add_url: 'check/cleaner/add',
                    edit_url: 'check/cleaner/edit',
                    del_url: 'check/cleaner/del',
                    multi_url: 'check/cleaner/multi',
                    table: 'cleaner',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-audit").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-assign").data("area", ["50%", "50%"]);
                $(".btn-d4").data("area", ["80%", "80%"]);
                $(".btn-d5").data("area", ["80%", "80%"]);
            });
            var check_id = [];
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                maintainSelected: 1,
                showSearch: false,
                search: false,
                searchFormTemplate: 'customformtpl',
                onCheck: function (row, tr, flied) {
                    check_id.push(row.id)
                    console.log(check_id);
                },
                onUncheck: function (row, tr, flied) {
                    // check_id.push(row.id)
                    console.log(check_id.indexOf(row.id));
                    var index = check_id.indexOf(row.id);
                    if(check_id.indexOf(row.id) !== -1){
                        check_id.splice(index, 1);
                    };
                    console.log(check_id);
                },
                columns: [
                    [
                        {
                            checkbox: true, formatter: function (value, row, index) {
                                //                                        if (row.check == 1) {
                                //                                            return { checked: true };
                                //                                        }

                                if (check_id.includes(row.id)) {
                                    return {checked: true};
                                }
                            }
                        },
                        {field: 'uid', title: __('用户ID'), sortable: true},
                        {field: 'cleaner_name', title: __('姓名'), sortable: true},
                        {field: 'phone', title: '联系电话', operate: 'like'},

                        {field: 'address', title: '所在地区', operate: false},

                        {field: 'housing', title: '派单地址', operate: 'like'},

                        {field: 'end_service_num', title: '本月完成单', operate: false},
                        {field: 'now_service_num', title: '当前接单量', operate: false},
                        {field: 'score', title: '评分', operate: false},
                        {
                            field: 'add_time',
                            title: __('注册时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: false,
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'status', title: __('状态'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "1": __('上线'),
                                "2": __('下线'),
                                "3": __('离职'),
                            },
                            formatter: function (value, row) {
                                if (value == 1) {
                                    return '<span style="">上线</span>';
                                } else if (value == 2) {
                                    return '<span style="">下线</span>';
                                } else if (value == 3) {
                                    return '离职';
                                }
                            }
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'check/cleaner/detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'up_status',
                                    text: __('上线'),
                                    title: __('上线'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'check/cleaner/change_status?status=1',
                                    confirm: '确定要上线该保洁员吗',
                                    success: function (data, ret) {
                                        Layer.alert('上线成功');
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },

                                },
                                {
                                    name: 'down_status',
                                    text: __('下线'),
                                    title: __('下线'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'check/cleaner/change_status?status=2',
                                    confirm: '确定要下线该保洁员吗',
                                    success: function (data, ret) {
                                        Layer.alert('下线成功');
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },

                                },
                                {
                                    name: 'leave_status',
                                    text: __('离职'),
                                    title: __('离职'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'check/cleaner/change_status?status=3',
                                    confirm: '确定要离职该保洁员吗',
                                    success: function (data, ret) {
                                        Layer.alert('离职成功');
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },

                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                if (row.status == 1) {
                                    $(table).data("operate-down_status", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                    $(table).data("operate-up_status", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                } else {
                                    $(table).data("operate-up_status", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                    $(table).data("operate-down_status", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-most-down", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');
                Fast.api.open('check/cleaner/do_most_down?check_id=' + check_id.join(",") + '', __('批量下线'), {area: ['50%', '50%']}, {
                    callback: function (value) {

                    }
                });
            });
            $(document).on("click", ".btn-most-up", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');

                var check_count = check_id.length;
                Layer.confirm(
                    __('保洁员账号被上线后将恢复接受分配服务单，请慎重操作！'),
                    {icon: 3, title: '确定要上线这' + check_count + '位保洁员吗', offset: 0, shadeClose: true},
                    function (index) {
                        up_cleaner(check_id.join(","));
                        Layer.close(index);
                    }
                );
            });
            $(document).on("click", ".btn-most-leave", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');

                var check_count = check_id.length;
                Layer.confirm(
                    __('保洁员与平台关系将变更为离职状态，请慎重操作！！'),
                    {icon: 3, title: '确定要离职这' + check_count + '位保洁员吗', offset: 0, shadeClose: true},
                    function (index) {
                        leave_cleaner(check_id.join(","));
                        Layer.close(index);
                    }
                );
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        do_most_down: function () {
            Controller.api.bindevent();
        },
        //审核
        audit: function () {
            Controller.api.bindevent();
            let audit_status1 = document.getElementById('row[audit_status]-1')
            let audit_status2 = document.getElementById('row[audit_status]-0')

            //监听是开票的情况
            audit_status1.addEventListener('click', function () {
                $('.bh').hide();
            });

            //监听不是开票的情况
            audit_status2.addEventListener('click', function () {
                $('.bh').show();
            });
        },
        //指派业务员
        do_assign: function () {
            Controller.api.bindevent();
            var is_have_saleman = Config.is_have_saleman;
            if (is_have_saleman == 1) {
                $('.saleman').show();
            } else {
                $('.saleman').hide();
            }
        },

        content: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'train/train/content',
                    add_url: 'train/train/add_content',
                    edit_url: 'train/train/edit_content',
                    del_url: 'train/train/del_content',
                    multi_url: 'train/train/multi',
                    table: 'content',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'train_title', title: '内培标题',},
                        {field: 'train.title', title: '类型名称',},
                        {
                            field: 'train_status', title: __('状态'),
                            searchList: {
                                "1": __('开启'),
                                "0": __('关闭'),
                            },
                            formatter: function (value) {
                                switch (value) {
                                    case 0:
                                        value = '关闭';
                                        break;
                                    case 1:
                                        value = '开启';
                                        break;

                                }
                                return value;
                            }
                        },
                        {
                            field: 'create_time',
                            title: __('添加时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                // {
                                //     name: 'city',
                                //     text: __('配置对应内容'),
                                //     title: __('配置对应内容'),
                                //     classname: 'btn btn-xs btn-success btn-magic btn-addtabs',
                                //     icon: 'fa fa-eye',
                                //     url: 'train/train/content?title_id={id}',
                                //     // extend:'data-area=["60%","70%"]',
                                //     callback: function (data) {
                                //     },
                                // },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add_content: function () {
            Controller.api.bindevent();
        },
        edit_content: function () {
            Controller.api.bindevent();
        },
        //选择校内班级
        select_school_class: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trusteeship/my_class/select_school_class' + location.search,
                }
            });


            var table = $("#table");
            // 初始化表格

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', visible: false, title: __('班级id'),},
                        {field: 'name', title: __('班级名称'), operate: false},
                    ]
                ]
            });

            // 同步微信小程序后台直播
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var urlArr = new Array();
                var now_arr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    //   now_arr=[j.goods_id,j.goods_name];
                    urlArr.push(j.id);
                    now_arr.push(j.name);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;

                Fast.api.close({id: urlArr.join(","), name: now_arr.join(","), multiple: true});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //选择班主任
        select_teacher: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trusteeship/my_class/select_teacher' + location.search,
                }
            });


            var table = $("#table");
            // 初始化表格
            var str = {
                1: "全国",
                2: "北京",
                3: "福建",
                4: "浙江",
                5: "河南",
                6: "安徽",
                7: "上海",
                8: "江苏",
                9: "山东",
                10: "江西",
                11: "重庆",
                12: "湖南",
                13: "湖北",
                14: "广东",
                15: "广西",
                16: "贵州",
                17: "海南",
                18: "四川",
                19: "云南",
                20: "陕西",
                21: "甘肃",
                22: "宁夏",
                23: "青海",
                24: "新疆",
                25: "西藏",
                26: "天津",
                27: "黑龙江",
                28: "吉林",
                29: "辽宁",
                30: "河北",
                31: "山西",
                32: "内蒙古"
            };

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', visible: false, title: __('id'),},
                        {field: 'nickname', title: __('班主任名称'), operate: false},
                    ]
                ]
            });

            // 同步微信小程序后台直播
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var urlArr = new Array();
                var now_arr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    //   now_arr=[j.goods_id,j.goods_name];
                    urlArr.push(j.id);
                    now_arr.push(j.nickname);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;

                Fast.api.close({id: urlArr.join(","), name: now_arr.join(","), multiple: true});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        //选择教室
        select_class_room: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trusteeship/my_class/select_class_room' + location.search,
                }
            });


            var table = $("#table");
            // 初始化表格
            var str = {
                1: "全国",
                2: "北京",
                3: "福建",
                4: "浙江",
                5: "河南",
                6: "安徽",
                7: "上海",
                8: "江苏",
                9: "山东",
                10: "江西",
                11: "重庆",
                12: "湖南",
                13: "湖北",
                14: "广东",
                15: "广西",
                16: "贵州",
                17: "海南",
                18: "四川",
                19: "云南",
                20: "陕西",
                21: "甘肃",
                22: "宁夏",
                23: "青海",
                24: "新疆",
                25: "西藏",
                26: "天津",
                27: "黑龙江",
                28: "吉林",
                29: "辽宁",
                30: "河北",
                31: "山西",
                32: "内蒙古"
            };

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', visible: false, title: __('id'),},
                        {field: 'classroom', title: __('教室名称'), operate: false},
                    ]
                ]
            });

            // 同步微信小程序后台直播
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var urlArr = new Array();
                var now_arr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    //   now_arr=[j.goods_id,j.goods_name];
                    urlArr.push(j.id);
                    now_arr.push(j.classroom);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;

                Fast.api.close({id: urlArr.join(","), name: now_arr.join(","), multiple: true});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //选择教室
        select_student: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reception/sheet/select_student' + location.search,
                }
            });


            var table = $("#table");
            // 初始化表格


            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', visible: false, title: __('id'),},
                        {field: 'student_no', title: '学号',},
                        {field: 'student_name', title: '学生名称'},
                        {field: 'phone', title: '手机号'},
                        {
                            field: 'sex', title: __('性别'),
                            searchList: {
                                "1": __('男'),
                                "2": __('女'),
                            }, formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        value = '男';
                                        break;
                                    case 2:
                                        value = '女';
                                        break;
                                }
                                return value;
                            }
                        },
                        {field: 'parents', title: '家长名称'},
                        {field: 'school_class', title: '家长名称', visible: false},
                        {field: 'school_class_id', title: '家长名称', visible: false},
                        {
                            field: 'image',
                            title: __('学生照片'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            operate: false
                        },
                    ]
                ]
            });

            // 同步微信小程序后台直播
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var urlArr = new Array();
                var now_arr = new Array();
                var phone_arr = new Array();
                var sex_arr = new Array();
                var parents_arr = new Array();
                var image_arr = new Array();
                var school_class_id_arr = new Array();
                var school_class_arr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    //   now_arr=[j.goods_id,j.goods_name];
                    urlArr.push(j.id);
                    now_arr.push(j.student_name);
                    phone_arr.push(j.phone);
                    sex_arr.push(j.sex);
                    image_arr.push(j.image);
                    parents_arr.push(j.parents);
                    school_class_id_arr.push(j.school_class_id);
                    school_class_arr.push(j.school_class);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;
                var new_data = {
                    id: urlArr.join(","),
                    student_name: now_arr.join(","),
                    phone: phone_arr.join(","),
                    sex: sex_arr.join(","),
                    parents: parents_arr.join(","),
                    image: image_arr.join(","),
                    school_class: school_class_arr.join(","),
                    school_class_id_arr: school_class_id_arr.join(","),
                    multiple: true
                };
                Fast.api.close(new_data);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

function up_cleaner(check_id) {
    Fast.api.ajax({
        url: 'check/cleaner/do_most_up',
        data: {'check_id': check_id}
    }, function (data, ret) {
        Fast.api.close(data);//在这里关闭当前弹窗
        parent.location.reload();//这里刷新父页面，可以换其他代码
        //   $('#table').bootstrapTable('refresh', {});
        Toastr.success('保存成功');
        //成功的回调
        return false;
    }, function (data, ret) {
        console.log(data);
        //失败的回调
        Toastr.error(ret.msg);
        return false;
    });
}

function leave_cleaner(check_id) {
    Fast.api.ajax({
        url: 'check/cleaner/do_most_leave',
        data: {'check_id': check_id}
    }, function (data, ret) {
        Fast.api.close(data);//在这里关闭当前弹窗
        parent.location.reload();//这里刷新父页面，可以换其他代码
        //   $('#table').bootstrapTable('refresh', {});
        Toastr.success('保存成功');
        //成功的回调
        return false;
    }, function (data, ret) {
        console.log(data);
        //失败的回调
        Toastr.error(ret.msg);
        return false;
    });
}